#include "netdev_manager.h"
#include "config_api.h"
#include "led_manager.h"
#include <netdb.h>
#include <netdev.h>
#include <netdev_ipaddr.h>
#include <rtdevice.h>
#include <rtthread.h>
#ifdef PKG_USING_PPP_DEVICE
#include "ppp_chat.h"
#include <ppp_run.h>
#endif

#include <sys/socket.h>

#define DBG_ENABLE
#define DBG_SECTION_NAME "netdev_mn"
#include "uc_log.h"

static int wifi_set_policy(char *name);

typedef int (*net_dev_func)(char *name);

typedef int (*net_set_policy)(char *name);

// 网卡选择策略
typedef struct netdev_select_policy
{
    // 网卡类型
    uc_netdev_policy_e type;
    char name[NETDEV_NAME_LEN_MAX];
    // 加载超时时间（加载间隔），不允许频繁的切换。
    short wait_timeout;
    // 默认加载标识，1 为加载， 0 为没有加载
    short flag;
    // 当前开始加载时间。
    unsigned int load_time;
    // 加载网卡后判断网络状态方法
    // net_set_policy judge;
    // 网卡网络状态
    enum netdev_cb_type net_status;
    // 打开设备
    net_dev_func on;
    // 关闭设备
    net_dev_func off;
} netdev_select_policy_t;

static uc_netdev_type_e g_netdev_type = ETH_DEV;

static netdev_callback_fn net_status_changed_callback_func = RT_NULL;

// 自动策略模式按照下面顺序执行优先级
netdev_select_policy_t g_netdev_select_policy[DEV_TYPE_MAX] = {
#ifdef BSP_USING_EMAC
    {ETH_DEV, NETDEV_INTERFACE, NETDEV_LOAD_WAIT_TIME, 1, 0 /*, netdev_set_policy*/, NETDEV_CB_STATUS_DOWN, NULL, NULL},
#endif
#ifdef RW007_USING_AT32_DRIVERS
    {WIFI_DEV, NETDEV_WIFI, WIFI_LOAD_WAIT_TIME, 0, 0 /*, netdev_set_policy*/, NETDEV_CB_STATUS_DOWN, wifi_on_func, wifi_off_func},
#endif
#ifdef PKG_USING_PPP_DEVICE
    {CAT1_DEV, NETDEV_CAT1, PPP_LOAD_WAIT_TIME, 0, 0 /*, netdev_set_policy*/, NETDEV_CB_STATUS_DOWN, cat1_on_func, cat1_off_func},
#endif
};
#ifdef RT_USING_WIFI
int wifi_off_func(char *name)
{
    if (RT_TRUE == rt_wlan_is_connected())
    {
        if (RT_NULL != gethostbyname(NETDEV_TEST_HOST))
        {
            TRACE_I("%s gethostbyname %s succ, no stop", __FUNCTION__, NETDEV_TEST_HOST);
            return 0;
        }
        TRACE_D("wlan close");
        rt_wlan_disconnect();
    }
    return 0;
}

int wifi_on_func(char *name)
{
    rt_err_t result;
    if (RT_TRUE != rt_wlan_is_connected())
    {
        gw_wifi_cfg_t *wifi_cfg = uc_static_get_gateway_wifi_cfg();

        if (rt_strlen(wifi_cfg->username) > 0)
        {
            result = rt_wlan_connect(wifi_cfg->username, wifi_cfg->password);
            if (result != RT_EOK)
            {
                TRACE_E("wifi connect failed");
                return -1;
            }
            // 等待wifi连接
            // rt_thread_delay(5000);
        }
        else
        {
            TRACE_E("wifi config is null");
            return -1;
        }
    }

    return 0;
}
#endif

#ifdef PKG_USING_PPP_DEVICE
int cat1_off_func(char *name)
{

#ifdef PPP_DEVICE_USING_ML305
    extern int get_ml305_prepare(void);
    // 关闭 ppp
    if (ppp_is_run() && 1 != get_ml305_prepare())
    {
        // cat1关闭时，判断网络是否可用。不可用才关闭（防止重复拨号）
        if (RT_NULL != gethostbyname(NETDEV_TEST_HOST))
        {
            TRACE_I("%s gethostbyname %s succ, no stop", __FUNCTION__, NETDEV_TEST_HOST);
            return 0;
        }

        TRACE_D("ppp close");
        ppp_stop();
    }
    return 0;
#elif defined(PPP_DEVICE_USING_AIR780EX)
    extern int get_air780_prepare(void);

    // 关闭 ppp
    if (ppp_is_run() && 1 != get_air780_prepare())
    {
        // cat1关闭时，判断网络是否可用。不可用才关闭（防止重复拨号）
        if (RT_NULL != gethostbyname(NETDEV_TEST_HOST))
        {
            TRACE_I("%s gethostbyname %s succ, no stop", __FUNCTION__, NETDEV_TEST_HOST);
            return 0;
        }

        TRACE_D("ppp close");
        ppp_stop();
    }
    return 0;
#elif defined(PPP_DEVICE_USING_EC800M)

    extern int get_ec800_prepare(void);

    // 关闭 ppp
    if (ppp_is_run() && 1 != get_ec800_prepare())
    {
        // cat1关闭时，判断网络是否可用。不可用才关闭（防止重复拨号）
        if (RT_NULL != gethostbyname(NETDEV_TEST_HOST))
        {
            TRACE_I("%s gethostbyname %s succ, no stop", __FUNCTION__, NETDEV_TEST_HOST);
            return 0;
        }

        TRACE_D("ppp close");
        ppp_stop();
    }
    return 0;

#else
    LOG_E("no ppp device");
#endif
}

int cat1_on_func(char *name)
{
    rt_uint16_t delay_s = 0;
    rt_bool_t state = ppp_is_run();
    TRACE_D("ready %s %d", name, state);
    if (RT_TRUE != state)
    {
        // TRACE_D("ppp_run");
        ppp_run();
        // 注册网络状态改变回调函数
        if (net_status_changed_callback_func != RT_NULL)
        {
            struct netdev *ndev = netdev_get_by_name(name);
            if (ndev != RT_NULL)
                netdev_set_status_callback(ndev, net_status_changed_callback_func);
        }
        // 等待拨号完成
        while (1)
        {
            state = ppp_is_run();
            if (RT_TRUE != state || delay_s > 30)
            {
                break;
            }
            rt_thread_mdelay(1000);
            delay_s += 1;
        }
    }

    return 0;
}
#endif

static int get_netdev_type(char *name)
{
    if (0 == rt_strncmp(name, NETDEV_INTERFACE, rt_strlen(NETDEV_INTERFACE)))
    {
        return ETH_DEV; // 以太网
    }
    else if (0 == rt_strncmp(name, NETDEV_WIFI, rt_strlen(NETDEV_WIFI)))
    {
        return WIFI_DEV; // wifi
    }
    else if (0 == rt_strncmp(name, NETDEV_CAT1, rt_strlen(NETDEV_CAT1)))
    {
        return CAT1_DEV; // cat1
    }
    else
    {
        return -1;
    }
}

int netdev_get_index_by_netdev(void *netdev)
{
    int index = 0;
    struct netdev *ndev = RT_NULL;
    for (index = 0; index < DEV_TYPE_MAX; index++)
    {
        if (netdev == netdev_get_by_name(g_netdev_select_policy[index].name))
            return index;
    }
    return -1;
}

int netdev_set_net_status(int index, unsigned int status)
{
    g_netdev_select_policy[index].net_status = status;
    return 0;
}

int netdev_set_status_changed_callback(void *callback)
{
    struct netdev *ndev;
    net_status_changed_callback_func = (netdev_callback_fn)callback;
    RT_ASSERT(net_status_changed_callback_func);

    ndev = netdev_get_by_name(g_netdev_select_policy[ETH_DEV].name);
    if (ndev != RT_NULL)
        netdev_set_status_callback(ndev, net_status_changed_callback_func);

    ndev = netdev_get_by_name(g_netdev_select_policy[WIFI_DEV].name);
    if (ndev != RT_NULL)
        netdev_set_status_callback(ndev, net_status_changed_callback_func);

    return RT_EOK;
}

// 设置路由
int netdev_set_policy(char *name)
{
    struct netdev *ndev;
    struct hostent *ip_addr;
    ip_addr_t dns_addr;

    ndev = netdev_get_by_name(name);
    if (ndev == RT_NULL)
    {
        TRACE_E("netdev_get_by_name %s failed", name);
        return -1;
    }

    // 判断网卡是否处于up状态
    if (!netdev_is_up(ndev))
    {
        TRACE_E("netdev_is_up %s failed", name);
        return -2;
    }

    if (!netdev_is_link_up(ndev))
    {
        TRACE_E("netdev_is_link_up %s failed", name);
        return -3;
    }

    // 设置选择的网卡为模式网卡
    netdev_set_default(ndev);

    inet_aton(CFG_PRIMARY_DNS, &dns_addr);
    netdev_set_dns_server(ndev, 0, &dns_addr);
    inet_aton(CFG_SECONDARY_DNS, &dns_addr);
    netdev_set_dns_server(ndev, 1, &dns_addr);

    if (0 == netdev_ip_get(ndev))
    { // 打印IP地址
        TRACE_E("local IP address %x", netdev_ip_get(ndev));
        return -5;
    }

    ip_addr = gethostbyname(NETDEV_TEST_HOST);
    if (RT_NULL == ip_addr)
    {
        TRACE_E("%s gethostbyname %s failed", name, NETDEV_TEST_HOST);
        return -6;
    }

    // TRACE_D("netdev_set_policy get baidu ip 0x%x", ip_addr->h_addr);

    return 0;
}

int netdev_led_event(uc_netdev_type_e net_type)
{
    switch (net_type)
    {
    case ETH_DEV:
        send_led_event(LED_CAT1, LED_E_EXCEPTION);
        send_led_event(LED_WIFI, LED_E_EXCEPTION);
        send_led_event(LED_ETH, LED_E_NORMAL);
        break;
    case WIFI_DEV:
        send_led_event(LED_CAT1, LED_E_EXCEPTION);
        send_led_event(LED_WIFI, LED_E_NORMAL);
        send_led_event(LED_ETH, LED_E_EXCEPTION);
        break;
    case CAT1_DEV:
        send_led_event(LED_CAT1, LED_E_NORMAL);
        send_led_event(LED_WIFI, LED_E_EXCEPTION);
        send_led_event(LED_ETH, LED_E_EXCEPTION);
        break;
    default:
        send_led_event(LED_CAT1, LED_E_EXCEPTION);
        send_led_event(LED_WIFI, LED_E_EXCEPTION);
        send_led_event(LED_ETH, LED_E_EXCEPTION);
    }
}

int netdev_internet_test(char *name)
{
    struct netdev *ndev;
    struct hostent *ip_addr;
    int dev_type = DEV_TYPE_MAX;
    int static_ip = 0;
    ip_addr_t dns1_addr;
    ip_addr_t dns2_addr;

    ndev = netdev_get_by_name(name);
    if (ndev == RT_NULL)
    {
        TRACE_E("netdev_get_by_name %s failed", name);
        return -1;
    }

    // 设置选择的网卡为默认网卡
    netdev_set_default(ndev);

    dev_type = get_netdev_type(name);

    if (dev_type == ETH_DEV || dev_type == WIFI_DEV)
    {
        if (dev_type == ETH_DEV)
        {
            gw_net_if_cfg_t *net_if_cfg = uc_static_get_gateway_net_if_cfg();
            static_ip = net_if_cfg->static_ip;
            inet_aton(net_if_cfg->primary_dns, &dns1_addr);
            inet_aton(net_if_cfg->secondary_dns, &dns2_addr);
        }
        else if (dev_type == WIFI_DEV)
        {
            gw_wifi_cfg_t *wifi_cfg = uc_static_get_gateway_wifi_cfg();
            static_ip = wifi_cfg->static_ip;
            inet_aton(wifi_cfg->primary_dns, &dns1_addr);
            inet_aton(wifi_cfg->secondary_dns, &dns2_addr);
        }

        if (static_ip == 0)
        {
            ip_addr = gethostbyname(NETDEV_TEST_HOST);
            if (RT_NULL == ip_addr)
            {
                netdev_dhcp_enabled(ndev, RT_FALSE);
                netdev_dhcp_enabled(ndev, RT_TRUE);
                TRACE_D("%s gethostbyname %s failed, try reset dhcp", name, NETDEV_TEST_HOST);
                rt_thread_mdelay(2000);
            }
            dns1_addr = ndev->gw;
            inet_aton(CFG_PRIMARY_DNS, &dns2_addr);
        }

        netdev_set_dns_server(ndev, 0, &dns1_addr);
        netdev_set_dns_server(ndev, 1, &dns2_addr);
        rt_thread_mdelay(2000);
    }

    ip_addr = gethostbyname(NETDEV_TEST_HOST_1);
    if (RT_NULL == ip_addr)
    {
        TRACE_E("%s gethostbyname %s failed", name, NETDEV_TEST_HOST_1);
    }
    else
    {
        goto __test_ok_exit;
    }

    ip_addr = gethostbyname(NETDEV_TEST_HOST_2);
    if (RT_NULL == ip_addr)
    {
        TRACE_E("%s gethostbyname %s failed", name, NETDEV_TEST_HOST_2);
    }
    else
    {
        goto __test_ok_exit;
    }

    ip_addr = gethostbyname(NETDEV_TEST_HOST_3);
    if (RT_NULL == ip_addr)
    {
        TRACE_E("%s gethostbyname %s failed", name, NETDEV_TEST_HOST_3);
    }
    else
    {
        goto __test_ok_exit;
    }

    if (RT_NULL == ip_addr)
    {
        TRACE_E("%s gethostbyname all failed", name);
        return -2;
    }

__test_ok_exit:

    TRACE_I("netdev %s internet test success", name);

    return 0;
}

// 设置默认网卡
int netdev_set_default_to_static_data(gw_net_policy_cfg_t *net_policy_cfg, char *name, uc_netdev_type_e net_type)
{
    netdev_led_event(net_type);
    // 保存已经加载的网卡到静态数据中.
    rt_strncpy(net_policy_cfg->netdev_name, name, sizeof(net_policy_cfg->netdev_name));

    TRACE_I("netdev %s set default success", name);

    return 0;
}

int netdev_reselect_policy(manager_net_e event)
{
    gw_net_policy_cfg_t *net_policy_cfg = uc_static_get_gateway_net_policy_cfg();
    uc_netdev_type_e net_type;
    int ret = 0;

    if (GATEWAY_WIFI_CONNECT == event)
    {
        // 第一次连接，将wifi名和密码发送到wifi模块中.
        g_netdev_select_policy[WIFI_DEV].on(g_netdev_select_policy[WIFI_DEV].name);
        // 关闭所有灯
        netdev_led_event(DEV_TYPE_MAX);
        // 等待wifi连接
        rt_thread_delay(5000);
    }

    if (NETDEV_MANUAL == net_policy_cfg->netdev_policy)
    {
        if (0 == netdev_internet_test(net_policy_cfg->netdev_name))
        {
            netdev_set_default_to_static_data(net_policy_cfg, net_policy_cfg->netdev_name, net_type);
        }
        else
        {
            net_type = get_netdev_type(net_policy_cfg->netdev_name);
            if (net_type == CAT1_DEV && NULL != g_netdev_select_policy[net_type].on)
            {
                g_netdev_select_policy[net_type].on(g_netdev_select_policy[net_type].name);
            }
        }
    }
    else
    {
        // 遍历网卡，找到第一个网络状态为网络连接的网卡
        for (net_type = 0; net_type < DEV_TYPE_MAX; net_type++)
        {
            if (g_netdev_select_policy[net_type].net_status == NETDEV_CB_STATUS_LINK_UP ||
                g_netdev_select_policy[net_type].net_status == NETDEV_CB_STATUS_INTERNET_UP)
            {
                if (0 == netdev_internet_test(g_netdev_select_policy[net_type].name))
                {
                    netdev_set_default_to_static_data(net_policy_cfg, g_netdev_select_policy[net_type].name, net_type);
                    break;
                }
            }
        }

        // 如果没有找到有网络连接的网卡，则打开cat1网卡
        if (net_type == DEV_TYPE_MAX && NULL != g_netdev_select_policy[CAT1_DEV].on)
        {
            g_netdev_select_policy[CAT1_DEV].on(g_netdev_select_policy[CAT1_DEV].name);
        }
    }

    return 0;
}

void netdev_enable_dhcp(char *netdev_name, unsigned char enable)
{
    struct netdev *netdev = RT_NULL;

    netdev = netdev_get_by_name(netdev_name);
    if (netdev == RT_NULL)
    {
        TRACE_E("bad network interface device name(%s)", netdev_name);
        return;
    }

    netdev_dhcp_enabled(netdev, enable);
}

static void netdev_set_if(char *netdev_name, char *ip_addr, char *gw_addr, char *nm_addr, char *p_dns, char *s_dns)
{
    struct netdev *netdev = RT_NULL;
    ip_addr_t addr;

    netdev = netdev_get_by_name(netdev_name);
    if (netdev == RT_NULL)
    {
        TRACE_E("bad network interface device name(%s)", netdev_name);
        return;
    }

    netdev_dhcp_enabled(netdev, RT_FALSE);
    rt_thread_mdelay(2000);

    /* set IP address */
    if ((ip_addr != RT_NULL) && inet_aton(ip_addr, &addr))
    {
        netdev_set_ipaddr(netdev, &addr);
    }

    /* set gateway address */
    if ((gw_addr != RT_NULL) && inet_aton(gw_addr, &addr))
    {
        netdev_set_gw(netdev, &addr);
    }

    /* set netmask address */
    if ((nm_addr != RT_NULL) && inet_aton(nm_addr, &addr))
    {
        netdev_set_netmask(netdev, &addr);
    }

    if (p_dns != RT_NULL && inet_aton(p_dns, &addr))
    {
        netdev_set_dns_server(netdev, 0, &addr);
    }

    if (s_dns != RT_NULL && inet_aton(s_dns, &addr))
    {
        netdev_set_dns_server(netdev, 1, &addr);
    }
}

void update_net_if_info(void)
{
    struct netdev *netdev = RT_NULL;
    struct netdev *default_netdev = netdev_get_default();
    gw_net_if_cfg_t *net_if_cfg = uc_static_get_gateway_net_if_cfg();
    int static_ip = net_if_cfg->static_ip;

    rt_memset(net_if_cfg, 0, sizeof(gw_net_if_cfg_t));
    net_if_cfg->static_ip = static_ip;

    netdev = netdev_get_by_name(NETDEV_INTERFACE);
    if (netdev)
    {
        if (0 == rt_strncmp(default_netdev->name, NETDEV_INTERFACE, rt_strlen(NETDEV_INTERFACE)))
        {
            rt_memcpy(net_if_cfg->netdev_state, DEFAULT_NETDEV_INTERFACE, rt_strlen(DEFAULT_NETDEV_INTERFACE));
        }
        else
        {
            rt_memcpy(net_if_cfg->netdev_state, NETDEV_INTERFACE, rt_strlen(NETDEV_INTERFACE));
        }
        rt_memcpy(net_if_cfg->mac, netdev->hwaddr, sizeof(net_if_cfg->mac));
        rt_memcpy(net_if_cfg->ip_addr, inet_ntoa(netdev->ip_addr.addr), sizeof(net_if_cfg->ip_addr));
        rt_memcpy(net_if_cfg->mask_addr, inet_ntoa(netdev->netmask.addr), sizeof(net_if_cfg->mask_addr));
        rt_memcpy(net_if_cfg->gateway_addr, inet_ntoa(netdev->gw.addr), sizeof(net_if_cfg->gateway_addr));
        rt_memcpy(net_if_cfg->primary_dns, inet_ntoa(netdev->dns_servers[0]), sizeof(net_if_cfg->primary_dns));
        rt_memcpy(net_if_cfg->secondary_dns, inet_ntoa(netdev->dns_servers[1]), sizeof(net_if_cfg->secondary_dns));
    }
}

void update_wifi_info(void)
{
    struct netdev *netdev = RT_NULL;
    struct netdev *default_netdev = netdev_get_default();
    gw_wifi_cfg_t *wifi_cfg = uc_static_get_gateway_wifi_cfg();
    int static_ip = wifi_cfg->static_ip;

    rt_memset(wifi_cfg, 0, sizeof(gw_wifi_cfg_t) - sizeof(wifi_cfg->username) * 2); // username和pwd不要清
    wifi_cfg->static_ip = static_ip;

    netdev = netdev_get_by_name(NETDEV_WIFI);
    if (netdev)
    {
        if (0 == rt_strncmp(default_netdev->name, NETDEV_WIFI, rt_strlen(NETDEV_WIFI)))
        {
            rt_memcpy(wifi_cfg->netdev_state, DEFAULT_NETDEV_WIFI, rt_strlen(DEFAULT_NETDEV_WIFI));
        }
        else
        {
            rt_memcpy(wifi_cfg->netdev_state, NETDEV_WIFI, rt_strlen(NETDEV_WIFI));
        }
        rt_memcpy(wifi_cfg->mac, netdev->hwaddr, sizeof(wifi_cfg->mac));
        rt_memcpy(wifi_cfg->ip_addr, inet_ntoa(netdev->ip_addr.addr), sizeof(wifi_cfg->ip_addr));
        rt_memcpy(wifi_cfg->mask_addr, inet_ntoa(netdev->netmask.addr), sizeof(wifi_cfg->mask_addr));
        rt_memcpy(wifi_cfg->gateway_addr, inet_ntoa(netdev->gw.addr), sizeof(wifi_cfg->gateway_addr));
        rt_memcpy(wifi_cfg->primary_dns, inet_ntoa(netdev->dns_servers[0]), sizeof(wifi_cfg->primary_dns));
        rt_memcpy(wifi_cfg->secondary_dns, inet_ntoa(netdev->dns_servers[1]), sizeof(wifi_cfg->secondary_dns));
    }
}
#ifdef PKG_USING_PPP_DEVICE
void update_cat1_info(void)
{
    struct netdev *netdev = RT_NULL;
    struct netdev *default_netdev = netdev_get_default();
    gw_cat1_cfg_t *cat1_cfg = uc_static_get_gateway_cat1_cfg();

    rt_memset(cat1_cfg, 0, sizeof(gw_cat1_cfg_t) - sizeof(cat1_cfg->apn)); // apn不要清
    struct cat_private_data *p_data = RT_NULL;
    p_data = rt_malloc(sizeof(struct cat_private_data));
    get_cat1_data(p_data);

    netdev = netdev_get_by_name(NETDEV_CAT1);
    if (netdev)
    {
        if (0 == rt_strncmp(default_netdev->name, NETDEV_CAT1, rt_strlen(NETDEV_CAT1)))
        {
            rt_memcpy(cat1_cfg->netdev_state, DEFAULT_NETDEV_CAT1, rt_strlen(DEFAULT_NETDEV_CAT1));
        }
        else
        {
            rt_memcpy(cat1_cfg->netdev_state, NETDEV_CAT1, rt_strlen(NETDEV_CAT1));
        }
        rt_memcpy(cat1_cfg->mac, netdev->hwaddr, sizeof(cat1_cfg->mac));
        rt_memcpy(cat1_cfg->ip_addr, inet_ntoa(netdev->ip_addr.addr), sizeof(cat1_cfg->ip_addr));
        rt_memcpy(cat1_cfg->mask_addr, inet_ntoa(netdev->netmask.addr), sizeof(cat1_cfg->mask_addr));
        rt_memcpy(cat1_cfg->gateway_addr, inet_ntoa(netdev->gw.addr), sizeof(cat1_cfg->gateway_addr));
        rt_strcpy(cat1_cfg->csq, p_data->csq);
        rt_strcpy(cat1_cfg->ccid, p_data->ccid);
        rt_strcpy(cat1_cfg->cimi, p_data->cimi);
        rt_strcpy(cat1_cfg->gsn, p_data->gsn);
    }
    rt_free(p_data);
    p_data = NULL;
}
#endif
void netdev_update_info(void)
{
    update_net_if_info();
    update_wifi_info();
#ifdef PKG_USING_PPP_DEVICE
    update_cat1_info();
#endif
}

// 设置静态ip、mask、gw
int netdev_set_static_ip(char *netdev_name, char ip_addr[16], char gateway_addr[16], char mask_addr[16], char p_dns[16], char s_dns[16])
{
    // gw_net_if_cfg_t *net_if_cfg = uc_static_get_gateway_net_if_cfg();
    // gw_wifi_cfg_t *wifi_cfg = uc_static_get_gateway_wifi_cfg();
    // int netdev_type = get_netdev_type(net_if_cfg->netdev_state);

    TRACE_I("net_if_set_static_ip %s:", netdev_name);
    TRACE_I("ip_addr %s", ip_addr);
    TRACE_I("gateway_addr %s", gateway_addr);
    TRACE_I("mask_addr %s", mask_addr);
    TRACE_I("primary_dns %s", p_dns);
    TRACE_I("secondary_dns %s", s_dns);

    // 静态ip配置
    netdev_set_if(netdev_name, ip_addr, gateway_addr, mask_addr, p_dns, s_dns);

    return 0;
}

int netdev_set_default_ip(void)
{
    gw_net_if_cfg_t *if_cfg = uc_static_get_gateway_net_if_cfg();
    gw_wifi_cfg_t *wifi_cfg = uc_static_get_gateway_wifi_cfg();

    if (if_cfg->static_ip)
    {
        netdev_set_static_ip(NETDEV_INTERFACE, if_cfg->ip_addr, if_cfg->gateway_addr,
                             if_cfg->mask_addr, if_cfg->primary_dns, if_cfg->secondary_dns);
    }

    if (wifi_cfg->static_ip)
    {
        netdev_set_static_ip(NETDEV_WIFI, wifi_cfg->ip_addr, wifi_cfg->gateway_addr,
                             wifi_cfg->mask_addr, wifi_cfg->primary_dns, wifi_cfg->secondary_dns);
    }

    return 0;
}

int netdev_set_macaddr(char *netdev_name, char *mac)
{
    struct netdev *netdev = RT_NULL;
    rt_device_t dev_device;

    netdev = netdev_get_by_name(netdev_name);
    if (netdev == RT_NULL)
    {
        TRACE_E("bad network interface device name(%s)", netdev_name);
        return -1;
    }

    dev_device = rt_device_find(netdev_name);
    if (dev_device == NULL)
    {
        TRACE_E("bad network dev name(%s)", netdev_name);
        return -2;
    }

    netdev_dhcp_enabled(netdev, RT_FALSE);
    // 将传入的MAC地址复制到网络设备结构体中
    for (int i = 0; i < 6; i++)
    {
        netdev->hwaddr[i] = mac[i];
    }
    netdev->hwaddr_len = 6;

    netdev_set_mac(netdev, mac);

    /* set hardware MAC address */
    rt_device_control(dev_device, 2, mac);

    netdev_dhcp_enabled(netdev, RT_TRUE);

    return 0;
}
